/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.util.Map;
import org.grnt.common.ContextWraper;
import org.grnt.common.OptStatBean;

/**
 *
 * This bean describes a buddy object, and it extends OptStatBean so that it has
 * operation stat.
 * @author sayid
 */
public class BuddyBean extends OptStatBean{
	final static Map<Long, UserDecorator> users = (Map<Long, UserDecorator>)ContextWraper.getAttribute("users");
	final static Map<Long, BuddyDecorator> buddies = (Map<Long, BuddyDecorator>)ContextWraper.getAttribute("buddies");
	final static Map<String, Long> usersIndex = (Map<String, Long>) ContextWraper.getAttribute("usersIndex");
	final static Map<String, Long> buddiesIndex = (Map<String, Long>) ContextWraper.getAttribute("buddiesIndex");
	
	Long    buddyId;
	String  alias;
	Long    parentId;
	Long    ownerId;
	Long	userId;
	Boolean isGroup;

	class Init {
		String alias;
		Long   parentId;
	}
	Init init = new Init();

	/**
	 *
	 * @return Buddy id.
	 */
	final public Long getBuddyId() {
		return this.buddyId;
	}

	/**
	 *
	 * @return alias of budd.
	 */
	final public String getAlias() {
		return this.alias;
	}

	/**
	 *
	 * @return Budd id of its parent.
	 */
	final public Long getParentId() {
		return this.parentId;
	}

	/**
	 *
	 * @return True when it is a group buddy.
	 */
	final public Boolean getIsGroup() {
		return this.isGroup;
	}

	/**
	 *
	 * @return
	 */
	final public Long getOwner() {
		return this.ownerId;
	}

	/**
	 *
	 * @return
	 */
	final public Long getUserId() {
		return this.userId;
	}

	/**
	 *
	 * @param alaise
	 * @param committed
	 */
	final public void setAlaise(String alaise, boolean committed) {
		this.alias = alaise;
	}

	/**
	 *
	 * @param parentId
	 * @param committed
	 */
	final public void setParentId(Long parentId, boolean committed) {
		this.parentId = parentId;
	}

	/**
	 *
	 * @param buddyId Buddy id
	 * @param alaise Buddy's alias
	 * @param parentId Buddy id of its parent
	 * @param owner User id who holds it.
	 * @param userId If the buddy isn't a group buddy this field represents the correct
	 * user which the buddy references to.
	 * @param isGroup Wether the buddy is a group buddy.
	 */
	final public void init(
			Long    buddyId
		,	String  alais
		,	Long    parentId
		,	Long    owner
		,	Long	userId
		,	Boolean isGroup
	) {
		this.buddyId  = buddyId;
		this.alias    = init.alias    = alais;
		this.parentId = init.parentId = parentId;
		this.isGroup  = isGroup;
		this.ownerId  = owner;
		this.userId   = userId;
	}

	@Override
	public void commit() throws OptStateException {
		if (!isActive()) {
			throw new OptStateException(msg);
		}
		init.alias = this.alias;
	}

	@Override
	public void rollback() throws OptStateException {
		if(!isActive()) {
			throw new OptStateException(msg);
		}
		this.alias = init.alias;
	}

	@Override
	public boolean isDirty() {
		return (this.alias.equals(init.alias) || this.parentId == init.parentId);
	}

	String msg = null;
	@Override
	public boolean isActive() {
		if (!super.isActive()){
			msg = "The buddy isn't an acvive object.";
		}

		if ((ownerId == null) || (ownerId == 0)) {
			msg = "The owner id is null or zero.";
		}

		if (!isGroup) {
			if((userId == null) || (userId == 0)) 
				msg = "The user id is null or zero.";
		} else {
			if ((userId != null) && (userId != 0))
				msg = "The group buddy's user id is not null.";
		}

		if ((userId != null) && (userId != 0)) {
			if (!users.containsKey(userId))
				msg = "The user id in the given buddy can't be found.";
		}

		if (!users.containsKey(ownerId)) {
			msg = "The owner id in the given buddy can't be found.";
		}

		if (parentId != null){
			if(!buddies.containsKey(parentId))
				msg = "The parent id in the given buddy can't be found.";
		} else {
			if(users.get(ownerId).getUserBean().getRootGroupId() != buddyId)
				msg = "The root group can't map to a correct owner.";
		}

		if (msg != null) {
			this.kill();
			return false;
		}else {
			return true;
		}
	}

	/**
	 *
	 * @return The mssage after checked.
	 */
	public String getChkMsg() {
		return msg;
	}
}
